class CodeFilesController < ApplicationController
  before_filter :find_widget
  
  # GET /code_files
  # GET /code_files.xml
  def index
    @code_files = @widget.code_files.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @code_files }
    end
  end

  # GET /code_files/new
  # GET /code_files/new.xml
  def new
    @code_file = @widget.code_files.build

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @code_file }
    end
  end

  # GET /code_files/1/edit
  def edit
    @code_file = @widget.code_files.find(params[:id])
  end

  # POST /code_files
  # POST /code_files.xml
  def create
    @code_file = @widget.code_files.build(params[:code_file])

    respond_to do |format|
      if @code_file.save
        flash[:notice] = 'CodeFile was successfully created.'
        format.html { redirect_to(widget_code_files_path(@widget)) }
        format.xml  { render :xml => @code_file, :status => :created, :location => @widget }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @code_file.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /code_files/1
  # PUT /code_files/1.xml
  def update
    @code_file = @widget.code_files.find(params[:id])

    respond_to do |format|
      if @code_file.update_attributes(params[:code_file])
        flash[:notice] = 'CodeFile was successfully updated.'
        format.html { redirect_to(widget_code_files_path(@widget)) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @code_file.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /code_files/1
  # DELETE /code_files/1.xml
  def destroy
    @code_file = @widget.code_files.find(params[:id])
    @code_file.destroy

    respond_to do |format|
      format.html { redirect_to(widget_code_files_path(@widget)) }
      format.xml  { head :ok }
    end
  end
  
  protected
  
  def find_widget
    @widget = Widget.find(params[:widget_id])
  end
end
